package com.yibo.yunpicture.interfaces.controller;

import cn.hutool.core.util.ObjUtil;
import com.yibo.yunpicture.interfaces.assembler.SpaceUserAssembler;
import com.yibo.yunpicture.shared.auth.annotation.SaSpaceCheckPermission;
import com.yibo.yunpicture.shared.auth.model.SpaceUserPermissionConstant;
import com.yibo.yunpicture.infrastructure.common.BaseResponse;
import com.yibo.yunpicture.infrastructure.common.DeleteRequest;
import com.yibo.yunpicture.infrastructure.common.ResultUtils;
import com.yibo.yunpicture.infrastructure.exception.ErrorCode;
import com.yibo.yunpicture.infrastructure.exception.ThrowUtils;
import com.yibo.yunpicture.interfaces.dto.spaceuser.SpaceUserAddDTO;
import com.yibo.yunpicture.interfaces.dto.spaceuser.SpaceUserEditDTO;
import com.yibo.yunpicture.interfaces.dto.spaceuser.SpaceUserQueryDTO;
import com.yibo.yunpicture.domain.space.entity.SpaceUser;
import com.yibo.yunpicture.domain.user.entity.User;
import com.yibo.yunpicture.interfaces.vo.space.SpaceUserVO;
import com.yibo.yunpicture.application.service.SpaceApplicationService;
import com.yibo.yunpicture.application.service.SpaceUserApplicationService;
import com.yibo.yunpicture.application.service.UserApplicationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/spaceUser")
@Slf4j
public class SpaceUserController {

    @Resource
    private SpaceUserApplicationService spaceUserApplicationService;

    @Resource
    private UserApplicationService userApplicationService;

    @Autowired
    private SpaceApplicationService spaceApplicationService;

    /**
     * 添加空间成员
     */
    @PostMapping("/add")
    // 必须有管理员权限
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.SPACE_USER_MANAGE)
    public BaseResponse<Long> addSpaceUser(@RequestBody SpaceUserAddDTO spaceUserAddDTO, HttpServletRequest request){
        ThrowUtils.throwIf(spaceUserAddDTO == null, ErrorCode.PARAMS_ERROR,"请求参数为空");
        long id = spaceUserApplicationService.addSpaceUser(spaceUserAddDTO);
        return ResultUtils.success(id);
    }

    /**
     * 从空间中移除成员
     */
    @PostMapping("/delete")
    // 必须有管理员权限
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.SPACE_USER_MANAGE)
    public BaseResponse<Boolean> removeSpaceUser(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request){
        return ResultUtils.success(spaceUserApplicationService.removeSpaceUser(deleteRequest));
    }

    /**
     * 查询某个成员在某个空间的信息（仅管理员查询）
     */
    @GetMapping("/get")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.SPACE_USER_MANAGE)
    public BaseResponse<SpaceUser > getSpaceUser(@RequestBody SpaceUserQueryDTO spaceUserQueryDTO){
        ThrowUtils.throwIf(spaceUserQueryDTO == null || spaceUserQueryDTO.getSpaceId() <= 0 || spaceUserQueryDTO.getUserId() <= 0, ErrorCode.PARAMS_ERROR,"请求参数为空");
        Long userId = spaceUserQueryDTO.getUserId();
        Long spaceId = spaceUserQueryDTO.getSpaceId();
        ThrowUtils.throwIf(ObjUtil.hasEmpty(userId,spaceId), ErrorCode.PARAMS_ERROR,"请求参数为空");
        // 查询数据库
        SpaceUser spaceUser = spaceUserApplicationService.getOne(spaceUserApplicationService.getQueryWrapper(spaceUserQueryDTO));
        ThrowUtils.throwIf(spaceUser == null, ErrorCode.NOT_FOUND_ERROR,"空间成员不存在");
        return ResultUtils.success(spaceUser);
    }

    /**
     * 查询空间成员列表
     */
    @PostMapping("/list")
    // 必须有管理员权限
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.SPACE_USER_MANAGE)
    public BaseResponse<List<SpaceUserVO>> listSpaceUser(@RequestBody SpaceUserQueryDTO spaceUserQueryDTO , HttpServletRequest request){
        ThrowUtils.throwIf(spaceUserQueryDTO == null || spaceUserQueryDTO.getSpaceId() <= 0, ErrorCode.PARAMS_ERROR,"请求参数为空");
        List<SpaceUser> spaceUserList = spaceUserApplicationService.list(spaceUserApplicationService.getQueryWrapper(spaceUserQueryDTO));
        List<SpaceUserVO> spaceUserVOList = spaceUserApplicationService.getSpaceUserVOList(spaceUserList);
        return ResultUtils.success(spaceUserVOList);
    }

    /**
     * 编辑成员信息（设置权限）
     */
    @PostMapping("/edit")
    // 必须有管理员权限
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.SPACE_USER_MANAGE)
    public BaseResponse<Boolean> editSpaceUser(@RequestBody SpaceUserEditDTO spaceUserEditDTO, HttpServletRequest request){
        ThrowUtils.throwIf(spaceUserEditDTO == null || spaceUserEditDTO.getId() <= 0, ErrorCode.PARAMS_ERROR,"请求参数为空");
        // 将实体类和 DTO 进行转换
        SpaceUser spaceUser = SpaceUserAssembler.toSpaceUserEntity(spaceUserEditDTO);
        // 数据校验
        spaceUserApplicationService.vaildSpaceUser(spaceUser, false);
        // 判断是否存在
        Long id = spaceUserEditDTO.getId();
        SpaceUser oldSpaceUser = spaceUserApplicationService.getById(id);
        ThrowUtils.throwIf(oldSpaceUser == null, ErrorCode.NOT_FOUND_ERROR,"空间成员不存在");
        // 数据库操作
        boolean result = spaceUserApplicationService.updateById(spaceUser);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    /**
     * 查询我加入的团队空间列表
     */
    @PostMapping("/list/my")
    public BaseResponse<List<SpaceUserVO>> listMyTeamSpace(HttpServletRequest request){
        User loginUser = userApplicationService.getLoginUser(request);
        SpaceUserQueryDTO spaceUserQueryDTO = new SpaceUserQueryDTO();
        spaceUserQueryDTO.setUserId(loginUser.getId());
        List<SpaceUser> spaceUserList = spaceUserApplicationService.list(spaceUserApplicationService.getQueryWrapper(spaceUserQueryDTO));
        return ResultUtils.success(spaceUserApplicationService.getSpaceUserVOList(spaceUserList));
    }

}
